﻿using PLCSharp.CoreModel.Prism;
using PLCSharp.Models;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;

namespace PLCSharp.Views.GlobalVariables
{
    [Model]
    public partial class VariablesModel : ModelBase
    {

        public VariablesModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
        {
       
            Thread thread = new(AutoSave)
            {
                IsBackground = true
            };
            thread.Start();
       
        }
 
        public GlobalModel GlobalModel { get; set; }
        public void Init(GlobalModel globalModel) {

            GlobalModel = globalModel;
        }
        private void AutoSave(object obj)
        {
            while (true)
            {
                Thread.Sleep(1000);
                _DatasContext.Save();

            }
        }
 
        public ObservableCollection<Variable> Variables { get; set; } = [];

        private Variable _SelectedVariable;
        public Variable SelectedVariable
        {
            get { return _SelectedVariable; }
            set { SetProperty(ref _SelectedVariable, value); }
        }


        private DelegateCommand _Add;
        public DelegateCommand Add =>
            _Add ??= new DelegateCommand(ExecuteAdd);

        void ExecuteAdd()
        {
            Variables.Add(new() { RecipeID = GlobalModel.CurrentRecipe.ID});
        }

        private DelegateCommand _Delete;
        public DelegateCommand Delete =>
            _Delete ??= new DelegateCommand(ExecuteDelete);

        void ExecuteDelete()
        {
            if (SelectedVariable != null)
            {
                _DatasContext.Variables.Remove(SelectedVariable);
                Variables.Remove(SelectedVariable);
                var name = SelectedVariable.Name;
                SendErr($"已删除变量：{name}", ErrType.ShowDialog);
            }

        }

        private DelegateCommand _Save;
        public DelegateCommand Save =>
            _Save ??= new DelegateCommand(ExecuteSave);

        void ExecuteSave()
        {
            var names = new List<string>();

            foreach (var item in Variables)
            {
                if (string.IsNullOrEmpty(item.Name))
                {
                    SendErr("保存失败，无变量名！", ErrType.ShowDialog);
                    return;
                }

                if (names.Contains(item.Name))
                {

                    SendErr("保存失败，重名的变量！", ErrType.ShowDialog);
                    return;

                }
                else
                {
                    names.Add(item.Name);
                }

            }


            foreach (var item in Variables)
            {
                if (!_DatasContext.Variables.Contains(item))
                {
                    _DatasContext.Variables.Add(item);
                }

            }
            SendErr("保存成功！", ErrType.ShowDialog);
        }



    }
}